<template>
  <div id="email">
    <el-form :model="emailForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item prop="username" label="账号">
        <el-input v-model="emailForm.username" ></el-input>
      </el-form-item>
      <el-form-item  prop="password" label="密码">
        <el-input type="password" v-model="emailForm.password" autocomplete="off" ></el-input>
      </el-form-item>
      <el-form-item  prop="checkPass" label="确认密码">
        <el-input type="password" v-model="emailForm.checkPass" autocomplete="off" ></el-input>
      </el-form-item>
      <el-form-item  prop="email" label="邮箱" @keyup.enter.native="getVerify">
        <el-input type="input" v-model="emailForm.email"></el-input>
      </el-form-item>
      <el-form-item prop="verify" label="验证码">
        <el-input  class="emailVerify" type="input" v-model="emailForm.verify"></el-input>
        <el-button class="sendEmail" v-if="btnShow" @click="getVerify">发送验证码</el-button>
        <el-button class="sendEmail" disabled v-if="!btnShow">{{ count }}s</el-button>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" class="subBtn" @click="submitForm('ruleForm')">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "EmailReg",
  data(){
    const validatePass = (rule, value, callback) => {
      if (value !== this.emailForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return{
      btnShow: true,
      count:"",
      timer:null,
      emailForm:{
        username:"",
        password:"",
        checkPass:"",
        email: "",
        verify: "",
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 5, max: 12, message: '长度在 5 到 12 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 12, message: '长度在 5 到 12 个字符', trigger: 'blur' }
        ],
        checkPass: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { validator: validatePass, trigger: ['blur','change'] }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'],},
        ],
        verify: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { min: 4, max: 4, message: '验证码为四位', trigger: ['blur','change'] }
        ]
      }
    }
  },
  methods:{
    getVerify() {
      // 验证邮箱
      if (this.checkEmail() === false) {
        return false;
      } else {
        console.log(this.emailForm.email)
        let param={
          email: this.emailForm.email
        }
        console.log(param)
        this.getRequest("/user/sendMailVerify?email="+this.emailForm.email).then(resp=>{
          console.log(resp)
        })
        const TIME_COUNT = 60; //更改倒计时时间
        if (!this.timer) {
          this.count = TIME_COUNT;
          this.btnShow = false;
          this.timer = setInterval(() => {
            if (this.count > 0 && this.count <= TIME_COUNT) {
              this.count--;
            } else {
              this.btnShow = true;
              clearInterval(this.timer); // 清除定时器
              this.timer = null;
            }
          }, 1000);
        }
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let param = {
            username: this.emailForm.username,
            email: this.emailForm.email,
            password: this.emailForm.password,
            checkPass: this.emailForm.checkPass,
            verify: this.emailForm.verify,
            phone: "",
          }

          this.postRequest("user/reg",param).then(resp => {
            this.$router.replace("/login");
          })
        }
      });
    },
    checkEmail() {
      let email = this.emailForm.email;
      if (!/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(email)) {
        this.$message.error("请填写正确的邮箱");
        return false;
      }
    },
  }
}
</script>

<style scoped>

.sendEmail{
  width: 100px;
  overflow: hidden;
}
.subBtn, .el-input{
  width: 350px;
}
.emailVerify{
  width: 250px;
}
</style>